//
//  ViewController.m
//  Demo0038
//
//  Created by zTercel on 13-4-13.
//  Copyright (c) 2013年 zTercel. All rights reserved.
//

#import "ViewController.h"

@interface ViewController (View)

@end

@implementation ViewController
{
    NSMutableArray              *filteredNames;
    UISearchDisplayController   *searchController;
}

@synthesize names;
@synthesize keys;


static NSString *cellIdentifier = @"SectionCellIndentifier";

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    UITableView *tableView  = (id)[self.view viewWithTag:1];
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
    
    NSString    *listPath   = [[NSBundle mainBundle]pathForResource:@"sortednames" ofType:@"plist"];
    names   = [[NSDictionary alloc]initWithContentsOfFile:listPath];
    keys    = [[names allKeys]sortedArrayUsingComparator:^(id obj1, id obj2) {
        return [obj1 compare:obj2];
    }];
    
    filteredNames               = [NSMutableArray array];
    UISearchBar *searchBar      = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
    tableView.tableHeaderView   = searchBar;
    
    searchController            = [[UISearchDisplayController alloc]initWithSearchBar:searchBar contentsController:self];
    searchController.delegate   = self;
    searchController.searchResultsDataSource    = self;
    
    return;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return (1 == tableView.tag) ? [keys count] : 1;
}

- (NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return (1 == tableView.tag) ? keys[section] : nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (1 == tableView.tag) ? [names[keys[section]] count] : [filteredNames count];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell   = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (1 == tableView.tag)
    {
        NSInteger   section = indexPath.section;
        NSInteger   row     = indexPath.row;
        NSString*   key     = keys[section];  // hold data of current section
        cell.textLabel.text = names[key][row];
    } else {
        cell.textLabel.text = filteredNames[indexPath.row];
    }
    return cell;
}

- (NSArray*)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    return (1 == tableView.tag) ? keys : nil;
}

- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentifier];
}

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [filteredNames removeAllObjects];
    if (searchString.length > 0) {
        NSPredicate  *predicate  = [NSPredicate predicateWithBlock: ^BOOL(NSString* name, NSDictionary *bindings) {
            NSRange range = [name rangeOfString:searchString options:NSCaseInsensitiveSearch];
            return (range.location != NSNotFound);
        }];
        
        for (NSString *section in keys) {
            NSArray *matchNames  = [names[section] filteredArrayUsingPredicate:predicate];
            [filteredNames addObjectsFromArray:matchNames];
        }
    }
    return YES;
}

@end
